package cn.hnu.qq.server.thread;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.HashMap;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.hnu.qq.entity.User;
import cn.hnu.qq.server.dao.CrowdDao;
import cn.hnu.qq.server.dao.UserDao;

/**
 * 
 * @author jared
 *
 */
public class AbstractClientThread {
	
	private static UserDao userDao;
	
	private static CrowdDao crowdDao;
	
	private static HashMap<String,String> maps = new HashMap<String, String>(); //在线人员列表 account-address
	
	private Logger log = Logger.getLogger("server");
	
	protected static HashMap<String,User> userinfos = new HashMap<String, User>(); //维护在线用户信息
	
	public Logger getLog() {
		return log;
	}

	public void setLog(Logger log) {
		this.log = log;
	}

	static {
		try { 
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); 
            DOMConfigurator.configure(AbstractClientThread.class.getResource("server.log4j.xml"));
            crowdDao = (CrowdDao)applicationContext.getBean("CrowdDao"); 
            userDao = (UserDao)applicationContext.getBean("UserDao"); 
        } catch (RuntimeException e) { 
        	System.err.println("AbstractClientThread持久层接口获取失败");
            e.printStackTrace(); 
        } 
	}
	
	private Socket socket;
	
	private ObjectInputStream input;
	
	private ObjectOutputStream output;
	
	public AbstractClientThread(Socket s) {
		this.socket = s;
	}

	public ObjectInputStream getInput() {
		System.out.println("AbstractClientThread获取input");
		try {
			if(input == null){
				input = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
			}
		} catch (Exception e) {
			System.err.println("服务端input获取失败");
		}
		return input;
	}


	public ObjectOutputStream getOutput() {
		System.out.println("AbstractClientThread获取output");
		try {
			if(output == null){
				output = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));
			}
		} catch (Exception e) {
			System.err.println("服务端input获取失败");
		}
		return output;
	}

	public synchronized void addOnlineUser(String account,String addr){
		maps.put(account, addr);
	}
	
	
	public User isUserOnline(String acount){
		
		return userinfos.get(acount);
		
	}
	
	public static UserDao getUserDao() {
		return userDao;
	}

	public static void setUserDao(UserDao userDao) {
		AbstractClientThread.userDao = userDao;
	}
	

	public static CrowdDao getCrowdDao() {
		return crowdDao;
	}

	public static void setCrowdDao(CrowdDao crowdDao) {
		AbstractClientThread.crowdDao = crowdDao;
	}

	public Socket getSocket() {
		return socket;
	}

	
	
}
